﻿using DeviceCommons.DeviceMessages.Abstractions;
using DeviceCommons.DeviceMessages.Models.V1;
using System.Buffers;

namespace DeviceCommons.DeviceMessages.Serialization.V1.Serializers
{
    public class DeviceMessageInfoReadingStatesSerializer : AbstractMessageSerializer<IDeviceMessageInfoReadingStates>, IDeviceMessageInfoReadingStatesSerializer
    {
        public DeviceMessageInfoReadingStatesSerializer() : base(new DeviceMessageInfoReadingStates())
        {
        }
        public override void Serializer(ArrayBufferWriter<byte> writer, IDeviceMessageInfoReadingStates model)
        {
            ArgumentNullException.ThrowIfNull(model);

            SerializeWithHeader(
                writer,
                headerLength: 1,
                writeHeaderAction: w => w.Write([model?.Count ?? 0]),
                serializeContentAction: w =>
                {
                    foreach (var state in model.StateArray ?? [])
                    {
                        new DeviceMessageInfoReadingStateSerializer().Serializer(w, state);
                    }
                });
        }
    }
}
